package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.QiniuUtil;
import cn.stylefeng.guns.sys.modular.system.entity.BtyBxda;
import cn.stylefeng.guns.sys.modular.system.entity.BtyZxBaoxiaodan;
import cn.stylefeng.guns.sys.modular.system.entity.ZxChengbenMingxi;
import cn.stylefeng.guns.sys.modular.system.entity.ZxHuopin;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyZxBaoxiaodanParam;
import cn.stylefeng.guns.sys.modular.system.model.params.ZxHuopinParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyZxBaoxiaodanResult;
import cn.stylefeng.guns.sys.modular.system.service.BtyBxdaService;
import cn.stylefeng.guns.sys.modular.system.service.BtyZxBaoxiaodanService;
import cn.stylefeng.guns.sys.modular.system.service.ZxChengbenMingxiService;
import cn.stylefeng.guns.sys.modular.system.service.ZxHuopinService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 控制器
 *
 * @author zxd
 * @Date 2022-04-22 09:09:41
 */
@Controller
@RequestMapping("/btyZxBaoxiaodan")
public class BtyZxBaoxiaodanController extends BaseController {

    private String PREFIX = "/modular/system/btyZxBaoxiaodan";

    @Autowired
    private BtyZxBaoxiaodanService btyZxBaoxiaodanService;

    @Resource
    private BtyBxdaService btyBxdaService;

    @Resource
    private ZxHuopinService zxHuopinService;

    private final String filePartPath = "/pictureUpload/project/zxbx/";

    private final String winPath = "D:" + filePartPath;

    private final String linuxPath = "/root" + filePartPath;


    /**
     * 跳转到主页面
     *
     * @author zxd
     * @Date 2022-04-22
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/btyZxBaoxiaodan.html";
    }

    /**
     * 新增页面
     *
     * @author zxd
     * @Date 2022-04-22
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/btyZxBaoxiaodan_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zxd
     * @Date 2022-04-22
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/btyZxBaoxiaodan_edit.html";
    }

    /**
     * 编辑页面
     *
     * @author zxd
     * @Date 2022-04-22
     */
    @GetMapping("/detail")
    public String detail() {
        return PREFIX + "/btyZxBaoxiaodan_detail.html";
    }

    /**
     * 新增接口
     *
     * @author zxd
     * @Date 2022-04-22
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BtyZxBaoxiaodanParam btyZxBaoxiaodanParam) {
        // 验证登陆用户是否有报销档案
        List<BtyBxda> btyBxdaList = btyBxdaService.bxdaList(LoginContextHolder.getContext().getUser().getAccount());
        if (CollectionUtils.isEmpty(btyBxdaList)) {
            return ResponseData.error("请先去【收款人维护】中，进行维护收款人信息后，在提交报销单");
        }
        String name = LoginContextHolder.getContext().getUser().getName();
        Long id = LoginContextHolder.getContext().getUser().getId();

        // 判断货品表是否存在记录
        ZxHuopinParam zxHuopinParam = new ZxHuopinParam();
        zxHuopinParam.setName(btyZxBaoxiaodanParam.getProductRemark());
        List<ZxHuopin> zxHuopinResultList = zxHuopinService.selectListByName(zxHuopinParam);
        String huopinId = "";
        if (zxHuopinResultList.size() == 0) {
            // 货品不存在
            zxHuopinParam.setGhs(btyBxdaList.get(0).getId().toString());
            zxHuopinParam.setGhsIds(btyBxdaList.get(0).getId().toString());
            zxHuopinParam.setGhsNames(btyBxdaList.get(0).getXm());
            zxHuopinParam.setHpType(2);
            huopinId = zxHuopinService.add1(zxHuopinParam);
        } else {
            huopinId = zxHuopinResultList.get(0).getBtyZxHuopinId();
        }

        // 报销单插入
        btyZxBaoxiaodanParam.setUserId(id);
        btyZxBaoxiaodanParam.setUserName(name);
        btyZxBaoxiaodanParam.setBxdaId(btyBxdaList.get(0).getId());
        btyZxBaoxiaodanParam.setHuopinId(huopinId);
        btyZxBaoxiaodanParam.setPayee(btyBxdaList.get(0).getXm());
        btyZxBaoxiaodanParam.setPayeeCard(btyBxdaList.get(0).getYhk());
        String[] arr = btyZxBaoxiaodanParam.getShopName().split("-");
        btyZxBaoxiaodanParam.setShopName(arr[0]);
        btyZxBaoxiaodanParam.setShopId(Long.parseLong(arr[1]));
        btyZxBaoxiaodanService.add(btyZxBaoxiaodanParam);

        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zxd
     * @Date 2022-04-22
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BtyZxBaoxiaodanParam btyZxBaoxiaodanParam) {
        // 验证登陆用户是否有报销档案
        List<BtyBxda> btyBxdaList = btyBxdaService.bxdaList(LoginContextHolder.getContext().getUser().getAccount());
        if (CollectionUtils.isEmpty(btyBxdaList)) {
            return ResponseData.error("请先去【收款人维护】中，进行维护收款人信息后，在提交报销单");
        }

        // 判断货品表是否存在记录
        ZxHuopinParam zxHuopinParam = new ZxHuopinParam();
        zxHuopinParam.setName(btyZxBaoxiaodanParam.getProductRemark());
        List<ZxHuopin> zxHuopinResultList = zxHuopinService.selectListByName(zxHuopinParam);
        String huopinId = "";
        if (zxHuopinResultList.size() == 0) {
            // 货品不存在
            zxHuopinParam.setGhs(btyBxdaList.get(0).getId().toString());
            zxHuopinParam.setGhsIds(btyBxdaList.get(0).getId().toString());
            zxHuopinParam.setGhsNames(btyBxdaList.get(0).getXm());
            zxHuopinParam.setHpType(2);
            huopinId = zxHuopinService.add1(zxHuopinParam);
        } else {
            huopinId = zxHuopinResultList.get(0).getBtyZxHuopinId();
        }

        btyZxBaoxiaodanParam.setHuopinId(huopinId);
        // 清除原因和驳回时间
        btyZxBaoxiaodanParam.setReason("");
        btyZxBaoxiaodanParam.setAuditTime(new Date(0));

        String[] arr = btyZxBaoxiaodanParam.getShopName().split("-");
        btyZxBaoxiaodanParam.setShopName(arr[0]);
        btyZxBaoxiaodanParam.setShopId(Long.parseLong(arr[1]));
        this.btyZxBaoxiaodanService.update(btyZxBaoxiaodanParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zxd
     * @Date 2022-04-22
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BtyZxBaoxiaodanParam btyZxBaoxiaodanParam) {
        this.btyZxBaoxiaodanService.delete(btyZxBaoxiaodanParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zxd
     * @Date 2022-04-22
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BtyZxBaoxiaodanParam btyZxBaoxiaodanParam) {
        BtyZxBaoxiaodan detail = this.btyZxBaoxiaodanService.getById(btyZxBaoxiaodanParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zxd
     * @Date 2022-04-22
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BtyZxBaoxiaodanParam btyZxBaoxiaodanParam) {
        btyZxBaoxiaodanParam.setUserId(LoginContextHolder.getContext().getUser().getId());
        return this.btyZxBaoxiaodanService.findPageBySpec(btyZxBaoxiaodanParam);
    }

    /**
     * 上传接口
     *
     * @author zxd
     * @Date 2022-04-22
     */
    @RequestMapping("/upload")
    @ResponseBody
    public Map<String, Object> upload(@RequestParam("file") MultipartFile file) {

        return QiniuUtil.uploadFile(file, null);
    }

    /**
     * 上传接口 直接保存数据库
     *
     * @author zxd
     * @Date 2022-08-07
     */
    @RequestMapping("/upload1")
    @ResponseBody
    public Map<String, Object> upload1(@RequestParam("file") MultipartFile file, Long id) {
        Map<String, Object> url =  QiniuUtil.uploadFile(file, null);
        Map<String, Object> data = (Map<String, Object>) url.get("data");
        String urlStr = data.get("src").toString();
        BtyZxBaoxiaodan btyZxBaoxiaodan = new BtyZxBaoxiaodan();
        btyZxBaoxiaodan.setFileUrl(urlStr);
        btyZxBaoxiaodan.setId(id);
        btyZxBaoxiaodanService.getBaseMapper().updateById(btyZxBaoxiaodan);
        return url;
    }

    /**
     * 供货商列表
     *
     * @author zxd
     * @Date 2022-04-22
     */
    @ResponseBody
    @RequestMapping("/ghsList")
    public List<BtyZxBaoxiaodanResult> ghsList() {
        return this.btyZxBaoxiaodanService.ghsList();
    }

    /**
     * 获取输入的统筹备注历史记录
     *
     * @author zxd
     * @Date 2022-04-29
     */
    @RequestMapping("/remarkHistory")
    @ResponseBody
    public ResponseData remarkHistory(BtyZxBaoxiaodanParam btyZxBaoxiaodanParam) {
        btyZxBaoxiaodanParam.setUserId(LoginContextHolder.getContext().getUser().getId());
        LayuiPageInfo list = this.btyZxBaoxiaodanService.findPageBySpec(btyZxBaoxiaodanParam);
        if (list.getData().size() > 0) {
            return ResponseData.success(list.getData().get(0));
        } else {
            return ResponseData.success();
        }
    }

    @Resource
    private ZxChengbenMingxiService zxChengbenMingxiService;
    @RequestMapping("/dingshi")
    @ResponseBody
    public ResponseData dingshi(BtyZxBaoxiaodanParam btyZxBaoxiaodanParam) {
        // 1.待提交，2.工程待审核，3.工程驳回，4.财务待审核，5.财务驳回，6.待打款，7.已打款；
        QueryWrapper qw = new QueryWrapper();
        qw.eq("status", 6);
        List<BtyZxBaoxiaodan> list = btyZxBaoxiaodanService.getBaseMapper().selectList(qw);
        if (list.size() > 0) {
            List ids = list.stream().map(BtyZxBaoxiaodan->BtyZxBaoxiaodan.getId()).collect(Collectors.toList());
            System.out.println(ids);
            QueryWrapper qw1 = new QueryWrapper();
            qw1.eq("status", 1);
            qw1.in("src_value", ids);
            List<ZxChengbenMingxi> list1 = zxChengbenMingxiService.getBaseMapper().selectList(qw1);
            if (list1.size() > 0) {
                List ids1 = list1.stream().map(ZxChengbenMingxi->ZxChengbenMingxi.getSrcValue()).collect(Collectors.toList());
                System.out.println(ids1);
                QueryWrapper qw2 = new QueryWrapper();
                qw2.in("id", ids1);
                BtyZxBaoxiaodan btyZxBaoxiaodan = new BtyZxBaoxiaodan();
                btyZxBaoxiaodan.setStatus(7);
                btyZxBaoxiaodanService.update(btyZxBaoxiaodan, qw2);
            } else {
                return ResponseData.error("成本明细表暂未查到报销表关联已打款的记录");
            }
        } else {
            return ResponseData.error("报销表未查到需同步的记录");
        }
        return ResponseData.success();
    }
}


